package com.quectel.cms.controller.device;


import cn.hutool.json.JSONUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.global.annotation.RequireTenants;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.device.dto.DeviceCommandTemplateDto;
import com.quectel.core.module.device.service.DeviceCommandTemplateService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-05-24 12:42:22
 */
@RestController
@RequestMapping("deviceCommandTemplate")
@Tag(name = "DeviceCommandTemplateController", description = "命令下发模板相关api")
public class DeviceCommandTemplateController extends BaseController {

    @DubboReference
    private DeviceCommandTemplateService deviceCommandTemplateService;


    /**
     * 获取所有
     */
    @GetMapping("queryAll")
    @Operation(summary = "根据条件获取命令下发模板列表")
    public Response<Object> queryAll(@RequestParam Map<String, Object> params) {
        //填充基础数据
        return Response.ok().wrap(deviceCommandTemplateService.queryList(params));
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "deviceCommandTemplate:config")
    @Operation(summary = "命令下发模板的分页查询列表")
    public Response<Page<DeviceCommandTemplateDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params);

        int total = deviceCommandTemplateService.queryTotal(params);

        List<DeviceCommandTemplateDto> list = null;
        if (total > 0) {
            list = deviceCommandTemplateService.queryList(params);
        } else {
            list = new ArrayList<>();
        }

        Page<DeviceCommandTemplateDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<DeviceCommandTemplateDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "deviceCommandTemplate:config")
    @Operation(summary = "查询单个命令下发模板记录")
    public Response<DeviceCommandTemplateDto> info(@PathVariable("id") Long id) {

        DeviceCommandTemplateDto deviceCommandTemplateDto = deviceCommandTemplateService.selectById(id);

        return Response.<DeviceCommandTemplateDto>ok().wrap(deviceCommandTemplateDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "deviceCommandTemplate:config")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "设备命令模板保存")
    @Log(express = "设备命令模板保存")
    public Response<Object> save(@RequestBody @Validated DeviceCommandTemplateDto deviceCommandTemplateDto) {

        DeviceCommandTemplateDto templateDto = deviceCommandTemplateService.selectByByCode(deviceCommandTemplateDto.getCommandCode());
        if (templateDto != null) {
            return Response.error("该编码已存在");
        }
        if (StringUtils.isNotBlank(deviceCommandTemplateDto.getTemplateBody())) {
            if (!JSONUtil.isJson(deviceCommandTemplateDto.getTemplateBody())) {
                return Response.error("命令模板格式错误");
            }
        } else {
            deviceCommandTemplateDto.setTemplateBody("{}");
        }

        //填充基础数据
        fillSaveCommonData(deviceCommandTemplateDto);

        deviceCommandTemplateService.save(deviceCommandTemplateDto);

        return Response.ok();
    }


    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "deviceCommandTemplate:config")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "设备命令模板修改")
    @Log(express = "设备命令模板修改")
    public Response<Object> update(@RequestBody @Validated DeviceCommandTemplateDto deviceCommandTemplateDto) {
        if (StringUtils.isNotBlank(deviceCommandTemplateDto.getTemplateBody())) {
            if (!JSONUtil.isJson(deviceCommandTemplateDto.getTemplateBody())) {
                return Response.error("命令模板格式错误");
            }
        } else {
            deviceCommandTemplateDto.setTemplateBody("{}");
        }
        //填充基础数据
        fillEditCommonData(deviceCommandTemplateDto);

        deviceCommandTemplateService.updateById(deviceCommandTemplateDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "deviceCommandTemplate:config")
    @Operation(summary = "设备命令模板删除")
    @Log(express = "设备命令模板删除")
    public Response<Object> delete(@RequestBody Long[] ids) {

        deviceCommandTemplateService.deleteBatch(ids);

        return Response.ok();
    }
}
